package com.potato.dashboard.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jofc2.model.Chart;
import jofc2.model.Text;
import jofc2.model.axis.XAxis;
import jofc2.model.axis.XAxisLabels;
import jofc2.model.elements.Tooltip;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.potato.dashboard.dao.ChartTemplateDao;
import com.potato.dashboard.model.ChartTemplate;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

@Controller
public class TempletController {
	@Autowired
	private ChartTemplateDao chartTemplateDao;
	
	@RequestMapping("/doAddTemplet.do")
	@Transactional
	public ModelAndView createTemplet(HttpServletRequest request){
		Chart chart = new Chart();
		Text title = new Text();
		String tstyle = "{font-size: "+request.getParameter("tsize")+"; font-family: Verdana; text-align: " + request.getParameter("talign")+ ";color: "+request.getParameter("hdcs1")+";}";
		title.setStyle(tstyle);
		title.setText(request.getParameter("tcontent"));
		chart.setBackgroundColour(request.getParameter("hdcs3"));
		chart.setTitle(title);
		
		XAxis xAxis = new XAxis();
		xAxis.setColour(request.getParameter("hdcs4"));
		xAxis.setGridColour(request.getParameter("hdcs5"));
		xAxis.setMax(10);
		xAxis.setMin(0);
		Double steps = Double.parseDouble(request.getParameter("xsteps"));
		xAxis.setSteps(steps);		
		xAxis.setStroke(4);
		xAxis.setTickHeight(2);
		XAxisLabels xlabel = new XAxisLabels();
		xlabel.setVisible(true);
		xlabel.setColour(request.getParameter("hdcs6"));
		xlabel.setSize(Integer.valueOf(request.getParameter("xlsize")));
		xAxis.setXAxisLabels(xlabel);
		chart.setXAxis(xAxis);
		
		Tooltip toolTip = new Tooltip();
		toolTip.setBackgroundColour("#ff5698");
		toolTip.setColour("#000000");
		toolTip.setStroke(5);
		toolTip.setColour("#ffffff");
		toolTip.setTitleStyle("{font-size: 20px; color: #CC2A43;}");
		toolTip.setBodyStyle("{font-size: 20px; color: #CC2A43;}");
		chart.setTooltip(toolTip);
		Text xLegend = new Text();
		xLegend.setText(request.getParameter("xtitle"));	
		xLegend.setStyle("{font-size: 30px;font-family:text-align: center;color: Verdana; color:#905050;}");
		chart.setXLegend(xLegend);
		
		XStream xStream = new XStream(new JettisonMappedXmlDriver());  
		xStream.setMode( XStream.NO_REFERENCES );  
		String content  = xStream.toXML(chart);
		
		ChartTemplate chartTemplet = new ChartTemplate();
		chartTemplet.setName(request.getParameter("name"));
		chartTemplet.setDescription(request.getParameter("desc"));
		
		chartTemplet.setContent(content);
		
		chartTemplateDao.save(chartTemplet);
		return list(); 
	}
	
	@RequestMapping("/list.do")
	public ModelAndView list() {
		List<ChartTemplate> all = chartTemplateDao.findAll();
		return new ModelAndView("/analyze/list", "all", all);
	}
	
	
}
